// pages/myDiary/myDiary.js
const util = require("../../utils/util")
const order = require("../../apis/order")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 5,
    pages: 0,
    count: 0,
    noData: false,
    loading: false,
    state: -1,
    stateName: '请选择',
    tag: 0,
    tagName: '我发布的',
    tagCols: ['我发布的', '我接收的'],
    stateCols: ['待接单', '待回收', '已回收', '已完成', '已取消', '已超时'],
    show: false,
    tagShow: false,
    stateShow: false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },

  async refresh() {
    await this.init(1);
  },

  choose(e) {
    const { type } = e.currentTarget.dataset
    if (type == 1) {
      this.setData({
        tagShow: true,
        type: type,
      })
    } else if (type == 2) {
      this.setData({
        stateShow: true,
        type: type,
      })
    }
  },

  onTagClose() {
    this.setData({
      tagShow: false,
    });
  },

  async onTagInput(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      tag: index,
      tagName: value,
      pageNum: 1 // 重置页码
    })
    const res = await this.init();
    this.onTagClose();
  },

  onStateClose() {
    this.setData({
      stateShow: false,
    });
  },

  async onStateInput(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      state: index,
      stateName: value,
      pageNum: 1 // 重置页码
    })
    const res = await this.init();
    this.onStateClose();
  },

  //初始化
  async init(type) {
    if (!type) {
      wx.showLoading({
        title: '加载中...',
      })
    }
    const param = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      state: this.data.state,
      tag: this.data.tag
    }
    let result = await order.listOrder(param);
    if (result.code == 200) {
      this.setData({
        list:  result.data.content.map(e => {
          return {
            id: e.id,
            employerName: e.employerName,
            state: util.getOrderState(e.state),
            subscriptionTime: e.subscriptionTime,
            address: e.address
          }
        }),
        pages: result.data.page,
        count: result.data.count
      })
      wx.hideLoading({
        success: (res) => { },
      })
    }
  },

  //下一页
  next() {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.init();
  },

  // 上一页
  pre() {
    this.setData({
      pageNum: this.data.pageNum - 1
    })
    this.init();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const info = JSON.parse(wx.getStorageSync("userInfo"))
    this.setData({ info: info })
    const { windowHeight, windowWidth } = app.globalData
    const backHeight = windowHeight - (windowWidth / 750) * 2 * 35
  
    this.setData({
      backHeight: backHeight,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
    this.setData({index:-1})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})